<?php
/**
 * 用户登陆
 * @author GL
 * @date 2018.8.23
 */
namespace App\Http\Controllers\Crm;

use App\Http\Controllers\BaseController;
use http\Env\Response;
use Illuminate\Http\Request;
use App\Model\Crm\UserModel;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Cookie;

class LoginController extends BaseController{


    //快速登录
    public function login(Request $request)
    {
        UserModel::forgetLogin();
        return view('/crm/login/login');
    }

    //用户登录
    public function checkLogin(Request $request)
    {
            $user_name = addslashes($request->get('user_name'));
            $password = addslashes($request->get('password'));
            $member = addslashes($request->get('member'));

            if(!$user_name){
               return self::error('0','用户名不能为空');
            }

            if(!$password){
                return self::error('0','密码不能为空');
            }

            $where['user_name'] = $user_name;

            $userInfo = UserModel::getUserInfo($where);
            if($userInfo){
                if(!$userInfo['is_enable']){
                    return self::error('login','用户未启用');
                }elseif ($userInfo['is_del']){
                    return self::error('login','该用户禁止登录');
                }else{
                    if($userInfo['password'] == pass_md5($userInfo['create_at'],$password)){
                        Session::put('user_id', $userInfo['id']);
                        Session::put('userInfo', $userInfo);
                        Session::save();
                        return self::success('1', $userInfo);
                    }
                    return self::error(1,'密码错误');
                }

            }else{
                return self::error('0','用户不存在');
            }

    }


    //修改密码
    public function changePassword(Request $request)
    {
        $user_id = $this->user_id;
        $old_pass = addslashes($request->get('old_pass'));
        $new_pass = addslashes($request->get('new_pass'));
        $re_pass = addslashes($request->get('re_pass'));
        if(!$old_pass){
            return self::error('0','请输入原密码');
        }

        if(!$new_pass){
            return self::error('0','请输入新密码');
        }

        if(!$re_pass){
            return self::error('0','请输入确定密码');
        }

        if($re_pass != $new_pass ){
            return self::error('0','两次输入密码不一致');
        }

        if($request->isMethod('post')){
            $where['id'] = $user_id;
            $info = UserModel::getUserInfo($where);
            if($info['password'] == pass_md5($info['create_at'],$old_pass)){
                $password = pass_md5($info['create_at'],$new_pass);
                $data['password'] = $password;
                $res = UserModel::updateUserInfo($where, $data);
                if($res){
                    return self::success('1','密码修改成功');
                }
                return self::error('0','密码修改失败，请稍后再试！！');
            }else{
                return self::error('0','原密码错误');
            }
        }
    }


    //用户退出
    public function logout(Request $request)
    {
        UserModel::forgetLogin();
        return redirect('/login');
    }






}